<?php
class item_message extends template_item
{
	var $default_name = "A Message";
	var $default_description = "Store a message. {status}";
	
	var $allow_multiple = 1;
	
	var $item_config = array();
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['unused'] = "No Message Stored, use item once to store a message.";
			$this->item_config['used'] = "A Message Stored";
		}
	}
	
	function admin_form()
	{
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"Tags for item name and description",
															"{status} = Status to state if a message is stored or not<br />Status Message is settable below.",
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Unused status message</b>",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][unused]", $this->item_config['unused']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Used status message</b>",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][used]", $this->item_config['used']),
													)	);
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		$save = array();
		$save['unused'] = $input['unused'];
		$save['used'] = $input['used'];
		
		$item_info['item_config'] = serialize($save);
	}
	
	function public_use_form($inventory_ids = array())
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$inv_id = reset($inventory_ids);
		$setting = unserialize($this->ipsclass->market_class->inventory_id[$inv_id]['settings']);
		
		if($setting['message'])
		{
			$this->delete_items($inventory_ids);
			$this->_write_log($inv_id, "Viewed the message");
			
			return <<<EOF
<div class='formsubtitle'>{$this->ipsclass->lang['saved_message']}</div>
	<table class='ipbtable' cellspacing='0' cellpadding='0'>
		<tr>
			<td class='row1' align='left' colspan='2'>
{$setting['message']}
			</td>
		</tr>
	</table>
EOF;
		}
		else
		{
	 		$this->ipsclass->han_editor->remove_side_panel = 1;
	 		$this->ipsclass->han_editor->remove_emoticons  = 1;
	        
	        $editor = $this->ipsclass->han_editor->show_editor( '', 'message' );
	        
			$hidden_fields = $this->_get_hidden_fields($inventory_ids);
			
			return <<<EOF
<div class='formsubtitle'>{$this->ipsclass->lang['store_a_message']}</div>
<form action='{$this->base_url}' method='post' id='changeform' onsubmit='return ValidateForm()'>
{$hidden_fields}
	<table class='ipbtable' cellspacing='0' cellpadding='0'>
		<tr>
			<td class='row1' align='left' colspan='2'>
{$editor}
			</td>
		</tr>
		<tr>
			<td class='darkrow1' align='center' colspan='2'>
				<input type='submit' name='item_used' value='{$this->ipsclass->lang['save_message']}' class='forminput' />
			</td>
		</tr>
	</table>
</form>
EOF;
		}
	}
	
	function input_done()
	{
		return isset($this->ipsclass->input['message']) && isset($this->ipsclass->input['item_used']);
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$inv_id = reset($inventory_ids);
		$message = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->han_editor->process_raw_post('message')));
		$settings = array('message' => $message);
		$this->ipsclass->DB->do_update('market_inventory',array(
																	'settings' => serialize($settings),
										),'i_id='.$inv_id		);
		
		$this->_write_log($inv_id, "Stored a message");
		
		$this->ipsclass->market_class->redirect('message_stored');
	}
	
	function parse_store_description()
	{
		return str_replace('{status}','',$this->item['item_description']);
	}
	
	function parse_group_description()
	{
		return str_replace('{status}','',$this->item['item_description']);
	}
	
	function parse_single_description($inv_item=array())
	{
		$setting = unserialize($inv_item['settings']);
		return str_replace('{status}',$setting['message'] ? $this->item_config['used'] : $this->item_config['unused'],$this->item['item_description']);
	}
}
?>